
jQuery.fileUpload = function(id,data,successfn){
	$.ajaxFileUpload({
		url:'file/uploadToAliOSS',
		secureuri:false,
		fileElementId:id,//file标签的id
		dataType: 'json',//返回数据的类型  
		data:data,//一同上传的数据  
		success: function (data, status) {
			successfn(data);
		},
		error: function (data, status, e) {
			alert(e);
		}
	});
}

jQuery.submitAjax = function(url,method,params,successfn){
	$.ajax({
		url :url,
        type : method,
        dataType: 'json',
        cache : false,
        timeout : 50000,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
        data:params,
        contentType:'application/json;charset=UTF-8',
        beforeSend:function(){},
        success : function(data) {
        	successfn(data);
        },
        error:function(){
        	$.messager.alert('提交','数据提交失败');
        },
        complete:function(XMLHttpRequest,status){
        	if(status=='timeout'){
        		ajax.abort();
        		$.messager.alert('提交','请求超时');
        	}
        }
    });
}
//创建窗口
var createWindown = function (url,title,width,height){
	if(width==null || width=='')width=300;
	if(height==null || height=='')height=200;
	$('<div></div>').dialog({
		id:'menu_win',
	    width:width,
	    height:height,
	    modal:true,
	    closed: false,
	    cache: false,
	    title:title,
	    href:url,
	    onClose : function() {
            $(this).dialog('destroy');
        },
	    buttons:[{
			text:'确认',
			iconCls:'icon-ok',
			handler:function(){
				$('#btn_sub').click();
			}
		},{
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				//关闭 不销毁
				$('#menu_win').dialog('close');
				//销毁
				//$('#menu_win').dialog('destroy');
			}
		}]
	});
}

//编辑时判断是否选择了需要编辑的数据
var createUpdateWindown = function(id,url,title,width,height){
	var rowsData = $('#'+id).datagrid('getSelected');
	if (!rowsData || rowsData.length==0) {
		$.messager.alert('编辑','请选择编辑记录');
		return false;
	}
	if (rowsData.length>1) {
		$.messager.alert('编辑','只能选择一条数据');
		return false;
	}
	url = url+'/'+rowsData.id;
	createWindown(url,title,width,height);
}

var createDelDialog = function (id,url,title,context){
	var rowsData = $('#'+id).datagrid('getSelected');
	if (!rowsData || rowsData.length==0) {
		$.messager.alert('删除','请选择需要删除的数据');
		return false;
	}
	if (rowsData.length>1) {
		$.messager.alert('删除','只能选择一条数据');
		return false;
	}
	url += '/'+rowsData.id;
	$.messager.confirm(title,context,function(r){
		if (r){
			$.submitAjax(url,'DELETE',{},function(data){
				if(data.success){
					console.log('删除成功');
					$('#'+id).datagrid('reload');
					$('#'+id).treegrid('reload');
				}else{
					$.messager.alert('消息','删除失败');
				}
			});
		}
	});
}